package com.friend.chat.page.view.main

import android.content.Intent
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.friend.chat.R
import com.friend.chat.base.BaseFragment
import com.friend.chat.chat.GroupChatActivity
import com.friend.chat.chat.SingleChatActivity
import com.friend.chat.databinding.FragmentContactsBinding
import com.friend.chat.page.bean.FriendBean
import com.friend.chat.page.bean.GroupBean
import com.friend.chat.page.bean.UserBean
import com.friend.chat.page.vm.MainViewModel
import com.friend.chat.utils.GlideUtils
import com.friend.chat.utils.SPUtils
import com.othershe.combinebitmap.CombineBitmap
import com.othershe.combinebitmap.layout.WechatLayoutManager


/**
 * @des
 * @author batie fancy
 * @date 2023/1/8
 */
class ContactsFragment : BaseFragment<FragmentContactsBinding, MainViewModel>() {

    private lateinit var mUserBean: UserBean
    private lateinit var mContactListAdapter: ContactListAdapter
    private lateinit var mGroupListAdapter: GroupListAdapter
    private var mUserId = ""

    override fun yourViewModel(): MainViewModel = MainViewModel()

    override fun yourViewBing(): FragmentContactsBinding {
        return FragmentContactsBinding.inflate(layoutInflater)
    }

    override fun init() {
        mUserBean = SPUtils.getInstance().getObject("user") as UserBean
        mUserId = mUserBean.uid

        val contactLinearLayoutManager = object : LinearLayoutManager(context) {
            override fun canScrollHorizontally(): Boolean {
                return false
            }

            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        mContactListAdapter = ContactListAdapter()
        mViewBing.rvContactList.layoutManager = contactLinearLayoutManager
        mViewBing.rvContactList.adapter = mContactListAdapter

        val groupLinearLayoutManager = object : LinearLayoutManager(context) {
            override fun canScrollHorizontally(): Boolean {
                return false
            }

            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        mGroupListAdapter = GroupListAdapter()
        mViewBing.rvGroupList.layoutManager = groupLinearLayoutManager
        mViewBing.rvGroupList.adapter = mGroupListAdapter
    }

    override fun onResume() {
        super.onResume()
        mVModel.getContactList(mUserId)
        mVModel.getGroupList(mUserId)
    }

    override fun setClick() {
        super.setClick()
        mContactListAdapter.setOnItemClickListener { adapter, _, position ->
            val friendBean = adapter.data[position] as FriendBean
            val intent = Intent(this.context, SingleChatActivity::class.java)
            intent.putExtra("userBean", mUserBean)
            intent.putExtra("friendBean", friendBean)
            startActivity(intent)
        }

        mGroupListAdapter.setOnItemClickListener { adapter, view, position ->
            val groupBean = adapter.data[position] as GroupBean
            GroupChatActivity.open(requireActivity(),mUserBean,groupBean)
        }
    }

    override fun onNetListener() {
        super.onNetListener()
        mVModel.contactListResponse.observe(this) {
            val data = it.data
            mContactListAdapter.setList(data)
        }

        mVModel.groupListResponse.observe(this) {
            val data = it.data
            mGroupListAdapter.setList(data)
        }
    }

    class ContactListAdapter :
        BaseQuickAdapter<FriendBean, BaseViewHolder>(R.layout.item_contact_list) {
        override fun convert(holder: BaseViewHolder, item: FriendBean) {
            val headerView = holder.getView<ImageView>(R.id.iv_item_contact_header)
            val nameView = holder.getView<TextView>(R.id.tv_item_contact_name)
            GlideUtils.loadRadiusImg1(headerView, item.friendHeader, 4)
            nameView.text = item.friendNickName
            addChildClickViewIds(R.id.rl_item_contact)
        }
    }

    class GroupListAdapter : BaseQuickAdapter<GroupBean, BaseViewHolder>(R.layout.item_group_list) {
        override fun convert(holder: BaseViewHolder, item: GroupBean) {
            val headerView = holder.getView<ImageView>(R.id.iv_item_group_header)
            val nameView = holder.getView<TextView>(R.id.tv_item_group_name)
            nameView.text = item.groupName

            val headerUrls = arrayOfNulls<String>(item.headers.size)
            for ((index, header) in item.headers.withIndex()) {
                headerUrls[index] = header.header
            }

            val builder = CombineBitmap.init(context)
                .setLayoutManager(WechatLayoutManager()) // 必选， 设置图片的组合形式，支持WechatLayoutManager、DingLayoutManager
                .setSize(40) // 必选，组合后Bitmap的尺寸，单位dp
                .setGap(3) // 单个图片之间的距离，单位dp，默认0dp
                .setGapColor(ContextCompat.getColor(context, R.color.bg_grey_1)) // 单个图片间距的颜色，默认白色
                //.setPlaceholder() // 单个图片加载失败的默认显示图片
                .setUrls(*headerUrls) // 要加载的图片url数组
                .setImageView(headerView) // 直接设置要显示图片的ImageView
                // 设置“子图片”的点击事件，需使用setImageView()，index和图片资源数组的索引对应
                .setOnSubItemClickListener { } // 加载进度的回调函数，如果不使用setImageView()方法，可在onComplete()完成最终图片的显示

            builder.build()

            addChildClickViewIds(R.id.rl_item_group)
        }
    }


}